package com.demo.page.controller.column;

import com.demo.common.entity.ColumnManagement;
import com.demo.common.entity.Meta;
import com.demo.page.feign.column.ColumnFeign;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询主栏目、次级栏目
 * 作者：孙佳佳、张兴然
 * 补充：张佑威
 * 分配任务：张改
 * 插入数据：贾冠博
 * 接口文档：贾冠博
 */
@RestController
@CrossOrigin
public class ColumnController {
    Map<String, Object> meta = new HashMap<>();

    Map<String, Object> dates = new HashMap<>();

    @Resource
    private ColumnFeign columnFeign;

    /**
     * 查询所有主栏目
     * @return
     */
    @GetMapping("/columns/selectAllFather")
    public Map search(){
        ResponseEntity<List<ColumnManagement>> listResponseEntity = columnFeign.searchAllParent();
        dates.put("column",listResponseEntity);
        meta.put("date", dates);
        if(listResponseEntity!=null){
            meta.put("meta", new Meta("获取成功", "200"));
        }else {
            meta.put("meta", new Meta("获取失败", "404"));
        }

        return meta;
    }

    @GetMapping("/columns/{columnFatherId}")
    public Map selectById(@PathVariable("columnFatherId") Integer columnFatherId){
        ResponseEntity<List<ColumnManagement>> listResponseEntity = columnFeign.searchByParentId(columnFatherId);
        dates.clear();
        dates.put("column",listResponseEntity);
        meta.put("date", dates);
        if(listResponseEntity!=null){
            meta.put("meta", new Meta("获取成功", "200"));
        }else {
            meta.put("meta", new Meta("获取失败", "404"));
        }

        return meta;
    }
}
